package com.zhxy.utils;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

//@Configuration  // 配置操作Redis的模板
public class RedisConfig {
        @Bean
        public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
            //创建一个模板对象，通过工厂对象来创建
            RedisTemplate<String,Object> template = new  RedisTemplate<String,Object>();
            //配置连接工厂
            template.setConnectionFactory(factory);
            //key采用String类型转换器
            template.setKeySerializer(new StringRedisSerializer());
            //hash的key使用String类型转换器
            template.setHashKeySerializer(new StringRedisSerializer());
            //Value的类型转换器
            template.setValueSerializer(new Jackson2JsonRedisSerializer(Object.class));
            //hash的value类型转换器
            template.setHashValueSerializer(new Jackson2JsonRedisSerializer(Object.class));

            return template;
        }
}
